package cc.gpai.util.slotboard;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;


@SuppressWarnings("serial")
public class MyLinkedBlockingDeque<E> extends LinkedList<E> implements BlockingDeque<E> {

	@Override
	public void put(E e) throws InterruptedException {
		add(e);
	}

	@Override
	public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
		return add(e);
	}

	@Override
	public E take() throws InterruptedException {
		return removeFirst();
	}

	@Override
	public E poll(long timeout, TimeUnit unit) throws InterruptedException {
		return removeFirst();
	}

	@Override
	public int remainingCapacity() {
		return size();
	}

	@Override
	public int drainTo(Collection<? super E> c) {
		LinkedList<E> l = new LinkedList<E>();
		l.addAll(this);
		clear();
		c.addAll(l);
		return l.size();
	}

	@Override
	public int drainTo(Collection<? super E> c, int maxElements) {
		if(maxElements<=0){
			return 0;
		}
		LinkedList<E> l = new LinkedList<E>();
		c.addAll(subList(0,Math.min(maxElements, size())));
		removeAll(l);
		c.addAll(l);
		return l.size();
	}

	@Override
	public void putFirst(E e) throws InterruptedException {
		addFirst(e);
	}

	@Override
	public void putLast(E e) throws InterruptedException {
		addLast(e);
	}

	@Override
	public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException {
		addFirst(e);
		return true;
	}

	@Override
	public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException {
		addLast(e);
		return true;
	}

	@Override
	public E takeFirst() throws InterruptedException {
		return removeFirst();
	}

	@Override
	public E takeLast() throws InterruptedException {
		return removeLast();
	}

	@Override
	public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException {
		return removeFirst();
	}

	@Override
	public E pollLast(long timeout, TimeUnit unit) throws InterruptedException {
		return removeLast();
	}

}
